﻿#define _CRT_SECURE_NO_WARNINGS

#include "Snake.h"

void SnakePrint(pSnakeNode ps)
{
	pSnakeNode cur = ps;
	while(cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
}

void SnakeCls(pSnakeNode ps)
{
	pSnakeNode cur = ps;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		printf("  ");
		cur = cur->next;
	}
}

//封装⼀个设置光标位置的函数
void SetPos(short x, short y)
{
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出的句柄

	COORD pos = { x, y };//设置光标位置
	SetConsoleCursorPosition(houtput, pos);
}

void WelcomeToGame()
{
	SetPos(38, 12);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(38, 20);
	system("pause");
	system("cls");
	SetPos(24, 12);
	wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动，按F3加速，F4减速\n");
	SetPos(24, 15);
	wprintf(L"加速能够得到更高分数\n");
	SetPos(38, 20);
	system("pause");
	system("cls");
}

void CreateMap()
{
	int i = 0;
	//上(00,00)-(56,00)
	SetPos(0, 0);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc",WALL);
	}
	//下(00,26)-(56,26)
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//左(00,01)-(00,25)
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右(56,01)-(56,25)
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

void SnakeInit(pSnake ps)
{
	//Snake.h文件中包含初始化蛇所需初始化的信息

	//1.蛇身初始化
	int i = 0;
	pSnakeNode cur = NULL;//创建一个蛇身类型的指针

	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));//为蛇身开辟空间
		if (cur == NULL)
		{
			perror("SnakeInit():malloc");
			exit(1);
		}
		cur->next = NULL;
		cur->x = POS_X + 2 * i;	//蛇x
		cur->y = POS_Y;			//蛇y

		//头插法插入链表
		if (ps->_pSnake == NULL)
		{//为空直接插入
			ps->_pSnake = cur;
		}
		else 
		{//非空表示已有头结点
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}//ps->_psnake->next = cur->next = 原来的ps->_pSnake,
		 //以此类推我们相当于先创建了蛇尾，最后再创建蛇头
		 //当然在这个过程中，ps->_pSnake一直都是头结点
	}

	SnakePrint(ps->_pSnake);

	//3.蛇的方向初始化
	ps->_Dir = RIGHT;

	//4.游戏状态初始化
	ps->_Status = OK;

	//5.游戏分数初始化
	ps->_Socre = 0;

	//6.食物得分初始化
	ps->_foodWeight = SCR;

	//7.休眠时间初始化
	ps->_SleepTime = TIME;//单位：ms	
}

void FoodCreate(pSnake ps)
{
	int x = 0, y = 0;//x:2-54	y:1-25

again:
	x = 2 * rand() % 26 + 2;
	y = rand() % 25 + 1;

	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		if (cur->x == x && cur->y == y)//避免食物与蛇身起冲突
		{
			goto again;
		}
		cur = cur->next;
	}

	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("FoodCreate():malloc");
		exit(1);
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;

	SetPos(x, y);
	wprintf(L"%lc", FOOD);

	ps->_pFood = pFood;
}

void GameInit(pSnake snake)
{
	//1.控制台窗⼝⼤⼩的设置
	system("mode con cols=100 lines=30");


	//2.控制台窗⼝名字的设置
	system("title 贪吃蛇");


	//3.⿏标光标的隐藏.
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出设备的句柄

	CONSOLE_CURSOR_INFO cursor_info = { 0 };//定义一个光标信息的结构体

	GetConsoleCursorInfo(houtput, &cursor_info);//获取控制台上的光标信息

	cursor_info.bVisible = false;//设置控制台上的光标的状态为隐藏

	SetConsoleCursorInfo(houtput, &cursor_info);//设置控制台上的光标信息

	
	//4.打印欢迎界⾯
	WelcomeToGame();
	
	//5.创建地图
	CreateMap();
	
	//6.初始化蛇
	SnakeInit(snake);

	//7.初始化食物
	FoodCreate(snake);

}

#define KEY_PRESS(vk)  ((GetAsyncKeyState(vk)&1)?1:0)//用来判断按键是否被按

void Pause()
{
	while(1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
			break;
	}
}

int KillBySelf(pSnake ps, int x, int y)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur->next)
	{
		if (cur->x == x && cur->y == y)
		{
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}

void EatFood(pSnake ps,int x,int y)
{//头插，不尾删
	pSnakeNode cur = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (cur == NULL)
	{
		perror("EatFood():malloc");
		exit(1);
	}

	//头插
	cur->next = ps->_pSnake;
	ps->_pSnake = cur;//现在的ps->_pSnake->next = cur->next = 原来的ps->_pSnake

	ps->_pSnake->x = x;
	ps->_pSnake->y = y;

	SnakePrint(ps->_pSnake);

	ps->_Socre += ps->_foodWeight;

	FoodCreate(ps);
}

void NormalMove(pSnake ps, int x, int y)
{//头插尾删
	pSnakeNode cur = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (cur == NULL)
	{
		perror("EatFood():malloc");
		exit(1);
	}

	cur->next = ps->_pSnake;
	ps->_pSnake = cur;

	ps->_pSnake->x = x;
	ps->_pSnake->y = y;
	//以上头插
	//以下尾删
	pSnakeNode s1 = ps->_pSnake;
	pSnakeNode s2 = ps->_pSnake;
	while (s1->next)
	{
		s2 = s1;
		s1 = s1->next;
	}
	free(s1);
	s1 = NULL;
	s2->next = s1;

	SnakePrint(ps->_pSnake);
}

void MoveSnake(pSnake ps)
{
	//先创建一个节点表示蛇下一个移动节点坐标
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("MoveSnake():malloc");
		exit(1);
	}
	
	//1.蛇移动的下个节点坐标
	switch (ps->_Dir)
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}

	//2.判断蛇下个节点状态
	if (pNextNode->x == ps->_pFood->x && pNextNode->y == ps->_pFood->y)//蛇吃到食物
	{
		SnakeCls(ps->_pSnake);
		EatFood(ps, pNextNode->x, pNextNode->y);
	}
	else if (pNextNode->x == 0 || pNextNode->x == 56
		|| pNextNode->y == 0 || pNextNode->y == 26)//蛇撞墙
	{
		ps->_Status = KILL_BY_WALL;
	}
	else if (KillBySelf(ps, pNextNode->x, pNextNode->y))//蛇撞到自己
	{
		ps->_Status = KILL_BY_SELF;
	}
	else//蛇正常移动
	{
		SnakeCls(ps->_pSnake);
		NormalMove(ps, pNextNode->x, pNextNode->y);
	}
}

void GameRun(pSnake ps)
{
	do 
	{
		//1.打印各种信息
		SetPos(64, 8);
		printf("总分数:%2d\n", ps->_Socre);
		SetPos(64, 10);
		printf("当前食物的分数:%2d\n", ps->_foodWeight);
		SetPos(64, 12);
		printf("用 ↑. ↓ . ← . → 来控制蛇的移动\n");
		SetPos(64, 13);
		printf("按F3加速，F4减速\n");
		SetPos(64, 15);
		printf("加速可以得到更高的分数\n");

		//2.确认各种状态
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)//判断↑是否被按
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)//判断↓是否被按
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)//判断→是否被按
		{
			ps->_Dir = RIGHT; 
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)//判断←是否被按
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_SPACE))//判断空格是否被按
		{
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))//判断ESC是否被按
		{
			ps->_Status = END_NORMAL;//正常退出
		}
		else if (KEY_PRESS(VK_F3))//判断F3
		{
			if (ps->_SleepTime > 100)
			{
				ps->_SleepTime -= 100;
				ps->_foodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))//判断F4
		{
			if (ps->_foodWeight > 2)
			{
				ps->_SleepTime += 100;
				ps->_foodWeight -= 2;
			}
		}

		//3.移动蛇
		MoveSnake(ps);

		Sleep(ps->_SleepTime);

	} while (ps->_Status == OK);
}

void GameEnd(pSnake ps)
{
	SetPos(24, 12);
	switch (ps->_Status)
	{
	case KILL_BY_WALL:
		printf("你撞到墙上，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("你撞到自己，游戏结束\n");
		break;
	case END_NORMAL:
		printf("您主动结束，游戏结束\n");
		break;
	}

	//释放蛇身链表
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode ptail = cur;
		cur = cur->next;
		free(ptail);
		ptail = NULL;
	}
}
